package com.sl.transport.repository.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.collection.ListUtil;
import cn.hutool.core.util.StrUtil;
import com.sl.transport.domain.OrganDTO;
import com.sl.transport.enums.OrganTypeEnum;
import com.sl.transport.repository.OrganRepository;
import org.neo4j.driver.types.Node;
import org.springframework.data.neo4j.core.Neo4jClient;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.Optional;

@Repository
public class OrganRepositoryImpl implements OrganRepository {

    @Resource
    private Neo4jClient neo4jClient;
    @Override
    public OrganDTO findByBid(Long bid) {
        String cypher = StrUtil.format("match (n) where n.bid = {} return n",bid);
        Optional<OrganDTO> optional = neo4jClient.query(cypher)
                .fetchAs(OrganDTO.class)
                .mappedBy(((typeSystem, record) -> {
                    Node node = record.get("n").asNode();
                    Map<String, Object> map = node.asMap();
                    OrganDTO organDTO = BeanUtil.toBeanIgnoreError(map, OrganDTO.class);

                    //机构类型
                    String label = CollUtil.getFirst(node.labels());
                    organDTO.setType(OrganTypeEnum.valueOf(label).getCode());

                    //经纬度
                    Object location = map.get("location");
                    organDTO.setLongitude(BeanUtil.getProperty(location, "x"));
                    organDTO.setLatitude(BeanUtil.getProperty(location, "y"));


                    return organDTO;
                })).one();
        return optional.orElse(null);
    }

    @Override
    public List<OrganDTO> findByBids(List<Long> bids) {
        String cypherQuery = StrUtil.format("match (n) where n.bid in {} return n",bids);

        return null;
    }

    @Override
    public List<OrganDTO> findAll(String name) {

        String cypherStr = null;
        if (StrUtil.isNotBlank(name)) {
            cypherStr = StrUtil.format("match (n) where n.name contains '{}' return n",name);
        } else {
            cypherStr = "match (n) return n";
        }
        Collection<OrganDTO> all = neo4jClient.query(cypherStr)
                .fetchAs(OrganDTO.class)
                .mappedBy(((typeSystem, record) -> {
                    Node node = record.get("n").asNode();
                    Map<String, Object> map = node.asMap();
                    OrganDTO organDTO = BeanUtil.toBeanIgnoreError(map, OrganDTO.class);

                    //机构类型
                    String label = CollUtil.getFirst(node.labels());
                    organDTO.setType(OrganTypeEnum.valueOf(label).getCode());

                    //经纬度
                    Object location = map.get("location");
                    organDTO.setLongitude(BeanUtil.getProperty(location, "x"));
                    organDTO.setLatitude(BeanUtil.getProperty(location, "y"));


                    return organDTO;
                })).all();
        return ListUtil.toList(all);
    }
}
